#include "SettingsScene.h"
#include "SimpleAudioEngine.h"
#include "WelcomeScene.h"
#include "SoundSettings.h"

using namespace cocos2d;
using namespace CocosDenshion;


CCScene* Settings::scene() {
	CCScene *scene = CCScene::create();
	Settings *layer = Settings::create();
	scene->addChild(layer);
	return scene;
}

bool Settings::init() {
	if (!CCLayer::init()) {
		return false;
	}
	CCSize size = CCDirector::sharedDirector()->getWinSize();

	CCMenuItemImage *pCloseItem = CCMenuItemImage::create("btn_back.png",
			"btn_back.png", this, menu_selector(Settings::goBack));
	pCloseItem->setPosition(ccp(size.width - 40, size.height-40));
	CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu, 1);

	CCLabelTTF* pLabel = CCLabelTTF::create("Settings", "Thonburi", 34);
	pLabel->setPosition(ccp(size.width / 2, size.height-40 ));
	this->addChild(pLabel, 1);

	float x = size.width / 2;
	float y = size.height / 2;
	float lx =100;
	CCLabelTTF* pL2 = CCLabelTTF::create("Music", "Thonburi", 34);
	pL2->setPosition(ccp(x-lx, y + 80));
	this->addChild(pL2, 1);

	CCMenuItemImage *pL2ii1 = CCMenuItemImage::create("btn_on.png",
			"btn_on.png", this, menu_selector(Settings::musicOn));
	CCMenuItemImage *pL2ii2 = CCMenuItemImage::create("btn_off.png",
			"btn_off.png", this, menu_selector(Settings::musicOff));
	CCMenu* pMenu2 = CCMenu::create(pL2ii1, pL2ii2, NULL);
	pMenu2->alignItemsHorizontally();
	pMenu2->setPosition(x + 30, y + 80);
	this->addChild(pMenu2);

	CCLabelTTF* pL3 = CCLabelTTF::create("Volume", "Thonburi", 34);

	int i = getBackgroundVolume()*10;
	char cVal[32];
	sprintf(cVal, "%d", i);

	CCLabelTTF* v=CCLabelTTF::create("0000", "Thonburi", 34);
	v->setString(cVal);
	mVolume = v;
	CCMenuItemImage *pL3ii1 = CCMenuItemImage::create("btn_up.png",
			"btn_up.png", this, menu_selector(Settings::musicUp));
	CCMenuItemLabel *pL3ii2 = CCMenuItemLabel::create(mVolume, this,
			menu_selector(Welcome::runSettings));
	CCMenuItemImage *pL3ii3 = CCMenuItemImage::create("btn_down.png",
			"btn_down.png", this, menu_selector(Settings::musicDown));

	CCMenu* pMenu3 = CCMenu::create(pL3ii1, pL3ii2, pL3ii3, NULL);
	pMenu3->alignItemsHorizontally();

	pL3->setPosition(ccp(x-lx, y + 20));
	pMenu3->setPosition(x + 30, y + 20);
	this->addChild(pL3, 1);
	this->addChild(pMenu3);

	CCLabelTTF* pL4 = CCLabelTTF::create("Sounds", "Thonburi", 34);
	pL4->setPosition(ccp(x-lx, y - 30));
	this->addChild(pL4, 1);

	CCMenuItemImage *pL4ii1 = CCMenuItemImage::create("btn_on.png",
			"btn_on.png", this, menu_selector(Settings::soundOn));
	CCMenuItemImage *pL4ii2 = CCMenuItemImage::create("btn_off.png",
			"btn_off.png", this, menu_selector(Settings::soundOff));
	CCMenu* pMenu4 = CCMenu::create(pL4ii1, pL4ii2, NULL);
	pMenu4->alignItemsHorizontally();
	pMenu4->setPosition(x + 30, y - 30);
	this->addChild(pMenu4);

	CCLabelTTF* pL5 = CCLabelTTF::create("Volume", "Thonburi", 34);
	pL5->setPosition(ccp(x-lx, y - 80));
	this->addChild(pL5, 1);

	int ia = getSoundVolume()*10;
	char cVal1[32];
	sprintf(cVal1, "%d", ia);
	CCLabelTTF* v2=CCLabelTTF::create("0000", "Thonburi", 34);
	v2->setString(cVal1);
	sVolume = v2;
	CCMenuItemImage *pL5ii1 = CCMenuItemImage::create("btn_up.png","btn_up.png", this, menu_selector(Settings::soundUp));
	CCMenuItemLabel *pL5ii2 = CCMenuItemLabel::create(sVolume, this,menu_selector(Welcome::runSettings));
	CCMenuItemImage *pL5ii3 = CCMenuItemImage::create("btn_down.png","btn_down.png", this, menu_selector(Settings::soundDown));
	CCMenu* pMenu5 = CCMenu::create(pL5ii1, pL5ii2, pL5ii3, NULL);
	pMenu5->alignItemsHorizontally();
	pMenu5->setPosition(x + 30, y- 80);
	this->addChild(pMenu5);
	return true;
}

void Settings::menuCloseCallback(CCObject* pSender) {
	CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
}
void Settings::goBack(CCObject* sender) {
	CCScene *pScene = Welcome::scene();
	CCDirector::sharedDirector()->replaceScene(pScene);
}
void Settings::musicOn(CCObject* sender) {
	music(true);
}
void Settings::musicOff(CCObject* sender) {
	music(false);
}
void Settings::soundOn(CCObject* sender) {
	sound(true);
}
void Settings::soundOff(CCObject* sender) {
	sound(false);
}
void Settings::sound(bool on) {
	shouldPlaySound(on);
	if (on) {
		playSound();
	} else {
		stopSound();
	}
}

void Settings::music(bool on) {
	shouldPlay(on);
	if (on) {
		playBackground();
	} else {
		stopMusic();
	}
}
void Settings::musicUp(CCObject* sender) {
	float current = getBackgroundVolume();
	current = current + 0.1f;
	setVolume(current);
	int i = current *10;
	if(i>9)
	{
		i=9;
	}
	char cVal[32];
	sprintf(cVal, "%d", i);
	mVolume->setString(cVal);
}
void Settings::musicDown(CCObject* sender) {
	float current = getBackgroundVolume();
	current = current -  0.1f;
	setVolume(current);
	int i = current *10;
	if(i<0){
		i=0;
	}
	char cVal[32];
	sprintf(cVal, "%d", i);
	mVolume->setString(cVal);
}
void Settings::soundUp(CCObject* sender) {
	float current = getSoundVolume();
	current = current + 0.1f;
	setSoundVolumne(current);
	int i = current *10;
	if(i>9)
	{
		i=9;
	}
	char cVal[32];
	sprintf(cVal, "%d", i);
	sVolume->setString(cVal);
}
void Settings::soundDown(CCObject* sender) {
	float current = getSoundVolume();
	current = current -  0.1f;
	setSoundVolumne(current);
	int i = current *10;
	if(i<0){
		i=0;
	}
	char cVal[32];
	sprintf(cVal, "%d", i);
	sVolume->setString(cVal);
}


